<?php

namespace App\Admin\Controllers;

use App\Jobs\RecordVideo;
use App\Jobs\Test;
use App\Videos;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Repos\FFMpeger;
use Illuminate\Support\Facades\Redis;

class VideoController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '视频页';

    protected $ffmpeg;

    public function __construct()
    {
        $this->ffmpeg = resolve('FFMpeger');
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Videos());

        $grid->column('id', __('Id'));
        $grid->column('video_name', __('Video name'));
        $grid->column('speaker_name', __('Speaker name'));
        $grid->column('coverurl', __('Coverurl'));
        $grid->column('play_count', __('Play count'));
        $grid->column('description', __('Description'));
        $grid->column('play_url', __('Play url'));
        $grid->column('download_url', __('Download url'));
        $grid->column('source_type', __('Source type'));
        $grid->column('iscutflag', __('Iscutflag'));
        $grid->column('duration', __('Duration'));
        $grid->column('filesize', __('Filesize'));
        $grid->column('school_id', __('School id'));
        $grid->column('create_user_name', __('Create user name'));
        $grid->column('class_id', __('Class id'));
        $grid->column('type', __('Type'));
        $grid->column('phase_id', __('Phase id'));
        $grid->column('subject_id', __('Subject id'));
        $grid->column('grade_id', __('Grade id'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Videos::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('video_name', __('Video name'));
        $show->field('speaker_name', __('Speaker name'));
        $show->field('coverurl', __('Coverurl'));
        $show->field('play_count', __('Play count'));
        $show->field('description', __('Description'));
        $show->field('play_url', __('Play url'));
        $show->field('download_url', __('Download url'));
        $show->field('source_type', __('Source type'));
        $show->field('iscutflag', __('Iscutflag'));
        $show->field('duration', __('Duration'));
        $show->field('filesize', __('Filesize'));
        $show->field('school_id', __('School id'));
        $show->field('create_user_name', __('Create user name'));
        $show->field('class_id', __('Class id'));
        $show->field('type', __('Type'));
        $show->field('phase_id', __('Phase id'));
        $show->field('subject_id', __('Subject id'));
        $show->field('grade_id', __('Grade id'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Videos());

        $form->text('video_name', __('Video name'));
        $form->text('speaker_name', __('Speaker name'));
        $form->text('coverurl', __('Coverurl'));
        $form->number('play_count', __('Play count'));
        $form->text('description', __('Description'));
        $form->text('play_url', __('Play url'));
        $form->text('download_url', __('Download url'));
        $form->switch('source_type', __('Source type'))->default(1);
        $form->switch('iscutflag', __('Iscutflag'));
        $form->text('duration', __('Duration'));
        $form->number('filesize', __('Filesize'));
        $form->number('school_id', __('School id'));
        $form->text('create_user_name', __('Create user name'));
        $form->number('class_id', __('Class id'));
        $form->switch('type', __('Type'));
        $form->number('phase_id', __('Phase id'));
        $form->number('subject_id', __('Subject id'));
        $form->number('grade_id', __('Grade id'));

        return $form;
    }

    public function montage(){
        $path1 = '/mnt/vod/1.mp4';
        $path2 = '/mnt/vod/2.mp4';
        $path3 = config('video.disks')[config('video.default')]['url'] . DIRECTORY_SEPARATOR . 'montage' . DIRECTORY_SEPARATOR;
        $path3 .= 'new.mp4';
        $ret = $this->ffmpeg->setTargetPath($path3)->montage($path1,$path2);
        echo '合并完成，新视频地址：'.$ret;
    }

    public function screenShot(){
        $videoPath = '/mnt/vod/2.mp4';
        $path3 = config('video.disks')[config('video.default')]['url'] . DIRECTORY_SEPARATOR . 'screenshot' . DIRECTORY_SEPARATOR;
        $path3 .= 'ss.jpg';
        // dump($path3);exit;
        $ret = $this->ffmpeg->setTargetPath($path3)->videoScreenShot($videoPath,20);
        // admin_success('数据处理成功.');
        echo '截图完成，图片地址：'.$ret;
    }

    public function watermark(){
        $path1 = '/mnt/vod/1.mp4';
        $path2 = '/mnt/vod/1.jpg';
        $path3 = config('video.disks')[config('video.default')]['url'] . DIRECTORY_SEPARATOR . 'watermark' . DIRECTORY_SEPARATOR;
        $path3 .= 'video_with_watermark.mp4';
        $ret = $this->ffmpeg->setTargetPath($path3)->videoWatermark($path1,$path2);
        echo '视频添加水印完成，新视频地址：'.$ret;
    }

    public function record_sync(){
        $rtmp = "rtmp://106.12.159.140:1935/live/stream";
        $path3 = config('video.disks')[config('video.default')]['url'] . DIRECTORY_SEPARATOR . 'record' . DIRECTORY_SEPARATOR;
        $path3 .= 'video_record.mp4';
        $duration = random_int(10,30);
        Redis::rpush('video-record', json_encode(['path'=>$path3,'rtmp'=>$rtmp,'duration'=>$duration]));
        
    }

    public function record_async(){
        $rtmp = "rtmp://106.12.159.140:1935/live/stream";
        $path3 = config('video.disks')[config('video.default')]['url'] . DIRECTORY_SEPARATOR . 'record' . DIRECTORY_SEPARATOR;
        $path3 .= 'video_record.mp4';
        $duration = random_int(10,20);
        dispatch(new RecordVideo($path3,$rtmp,$duration));
    }

    

    

}
